package com.zhao.dao.impl;

import com.zhao.dao.IBlackListDao;
import com.zhao.entity.BlackList;
import com.zhao.utils.DBCPUtil;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.List;

/**
 * Description: <br/>
 *
 * @author 柒柒
 * @version : 1.0
 * @ClassName BlackListDaoImpl <br/>
 * Copyright (c) ，2020 ， 赵 <br/>
 * Date： 2020/12/11 16:55
 */

public class BlackListDaoImpl implements IBlackListDao {
    private QueryRunner qr = new QueryRunner(DBCPUtil.getConnectionPool());


    @Override
    public void saveOrUpdate(BlackList entity) {
        try {
            qr.update("insert into tb_realtime_game value(?,?,?) on duplicate key update avgTime=?,cnt=cnt+1");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("操作开了外挂的玩家信息失败!,异常信息是:"+ e.getMessage());
        }
    }

    @Override
    public void batchDealWith(List<BlackList> beans) {
        try {
            String sql = "insert into tb_realtime_gamelog value(?,?,?) on duplicate key update avgTime=?,cnt=cnt+1";
            Object[][] params = new Object[beans.size()][];

            for (int i = 0; i <params.length ; i++) {
                BlackList bean = beans.get(i);
                params[i] = new Object[]{
                        bean.getUserName(),bean.getAvgTime(),bean.getCnt(),bean.getAvgTime()
                };
            }

            //将sql送往db server去批量执行
            qr.batch(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("操作开了外挂的玩家信息失败!,异常信息是:" + e.getMessage());
        }
    }

}
